#include "departmentchangeview.h"
#include "adminmanager.h"
#include <QDebug>

DepartmentChangeView::DepartmentChangeView(QWidget *parent) : QWidget(parent),
    m_adminManager(nullptr),
    m_tableWidget(nullptr),
    m_approveButton(nullptr),
    m_rejectButton(nullptr),
    m_titleLabel(nullptr)
{
    initUI();
}

DepartmentChangeView::~DepartmentChangeView()
{
    // 析构函数，清理资源
}

void DepartmentChangeView::setAdminManager(AdminManager *manager)
{
    m_adminManager = manager;
    // 连接信号槽
    connect(m_adminManager, &AdminManager::operationResult, this, [this](bool success, const QString &message) {
        QMessageBox::information(this, success ? "成功" : "失败", message);
        if (success) {
            // 操作成功后刷新数据
            refreshTransferRequests();
        }
    });
    // 初始加载数据
    refreshTransferRequests();
}

void DepartmentChangeView::refreshTransferRequests()
{
    if (!m_adminManager) {
        QMessageBox::warning(this, "警告", "AdminManager未设置");
        return;
    }

    // 清空表格
    m_tableWidget->clearContents();
    m_tableWidget->setRowCount(0);

    // 获取待审核的调岗申请
    QList<QVariantList> requests = m_adminManager->getPendingTransferRequests();

    // 填充表格
    for (int i = 0; i < requests.size(); ++i) {
        const QVariantList &request = requests[i];
        int row = m_tableWidget->rowCount();
        m_tableWidget->insertRow(row);

        // 设置表格内容
        for (int j = 0; j < request.size() && j < m_tableWidget->columnCount(); ++j) {
            QTableWidgetItem *item = new QTableWidgetItem(request[j].toString());
            item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 不可编辑
            m_tableWidget->setItem(row, j, item);
        }
    }

    // 自动调整列宽
    m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void DepartmentChangeView::onApproveButtonClicked()
{
    int currentRow = m_tableWidget->currentRow();
    if (currentRow < 0) {
        QMessageBox::warning(this, "警告", "请选择一条申请进行操作");
        return;
    }

    QTableWidgetItem *item = m_tableWidget->item(currentRow, 0);
    if (!item) {
        QMessageBox::warning(this, "警告", "无法获取申请ID");
        return;
    }

    QString requestId = item->text();
    if (QMessageBox::question(this, "确认", QString("确定要同意申请 %1 吗?").arg(requestId),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        m_adminManager->approveTransferRequest(requestId);
    }
}

void DepartmentChangeView::onRejectButtonClicked()
{
    int currentRow = m_tableWidget->currentRow();
    if (currentRow < 0) {
        QMessageBox::warning(this, "警告", "请选择一条申请进行操作");
        return;
    }

    QTableWidgetItem *item = m_tableWidget->item(currentRow, 0);
    if (!item) {
        QMessageBox::warning(this, "警告", "无法获取申请ID");
        return;
    }

    QString requestId = item->text();
    if (QMessageBox::question(this, "确认", QString("确定要拒绝申请 %1 吗?").arg(requestId),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        m_adminManager->rejectTransferRequest(requestId);
    }
}

void DepartmentChangeView::initUI()
{
    // 设置窗口标题
    setWindowTitle("部门调换审核");

    // 创建标题标签
    m_titleLabel = new QLabel("部门调换申请审核");
    QFont font;
    font.setPointSize(14);
    font.setBold(true);
    m_titleLabel->setFont(font);
    m_titleLabel->setAlignment(Qt::AlignCenter);

    // 创建表格
    m_tableWidget = new QTableWidget;
    m_tableWidget->setColumnCount(10);
    QStringList headers;
    headers << "申请ID" << "员工ID" << "原部门ID" << "新部门ID" << "申请原因" << "申请时间" << "状态" << "员工姓名" << "原部门名称" << "新部门名称";
    m_tableWidget->setHorizontalHeaderLabels(headers);
    m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); // 选择整行
    m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑

    // 创建按钮
    m_approveButton = new QPushButton("同意");
    m_rejectButton = new QPushButton("拒绝");

    // 连接按钮信号槽
    connect(m_approveButton, &QPushButton::clicked, this, &DepartmentChangeView::onApproveButtonClicked);
    connect(m_rejectButton, &QPushButton::clicked, this, &DepartmentChangeView::onRejectButtonClicked);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(m_approveButton);
    buttonLayout->addWidget(m_rejectButton);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(m_titleLabel);
    mainLayout->addWidget(m_tableWidget);
    mainLayout->addLayout(buttonLayout);

    // 设置主布局
    setLayout(mainLayout);
    resize(1000, 600);
}
